1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| #include <dht11.h> #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <DS1302.h> #include<IRremote.h>
#define dht11Pin 8 #define ledBlue 6 #define ledRed 7 #define beep 5 dht11 dht; char buf1[50]; char buf2[50]; LiquidCrystal_I2C lcd(0x27,16,2); DS1302 rtc(14, 15, 16); int RECV_PIN = 11; int Keynum=1; IRrecv irrecv(RECV_PIN); decode_results results;
void initRTCTime(void) { rtc.writeProtect(false); rtc.halt(false); Time t(2022, 12, 19, 16, 25, 50, 4); rtc.time(t); } void printTime() { Time tim = rtc.time(); snprintf(buf1, sizeof(buf1), "%04d-%02d-%02d ", tim.yr, tim.mon, tim.date ); snprintf(buf2, sizeof(buf2), "%02d:%02d:%02d", tim.hr, tim.min, tim.sec);
Serial.println(buf1); Serial.println(buf2);
} void setup() { Serial.begin(9600); pinMode(dht11Pin, OUTPUT); pinMode(ledBlue, OUTPUT); pinMode(ledRed, OUTPUT); pinMode(beep, OUTPUT);
lcd.init(); lcd.backlight(); initRTCTime(); irrecv.enableIRIn(); delay(2000); }
void loop() { if (irrecv.decode(&results)!=0) { delay(500); Keynum++; delay(500); lcd.clear(); if(Keynum>=3) { Keynum=1; } irrecv.resume(); } switch(Keynum) { case 1: SerialTem();ther();TemJudge();alarm();break; case 2: printTime();TemJudge(); Time tim = rtc.time(); lcd.setCursor(0,0); lcd.print(buf1); lcd.setCursor(0,1); lcd.print(buf2); alarm(); break; default: break; } } void ther() { int tol = dht.read(dht11Pin); int temp = (float)dht.temperature; int humi = (float)dht.humidity;
lcd.setCursor(0,0); lcd.print("Tem:"); lcd.setCursor(4,0); lcd.print(temp); lcd.setCursor(6,0); lcd.print("C");
lcd.setCursor(0,1); lcd.print("Hum:"); lcd.setCursor(4,1); lcd.print(humi); lcd.setCursor(6,1); lcd.print("%"); } void TemJudge() { int tol = dht.read(dht11Pin); int temp = (float)dht.temperature; int humi = (float)dht.humidity; if(temp>=20) { digitalWrite(ledBlue,HIGH); } else digitalWrite(ledBlue,LOW); if(humi>=34) { digitalWrite(ledRed,HIGH); } else digitalWrite(ledRed,LOW);
} void alarm() { digitalWrite(beep,HIGH); int alarm_hr=16; int alarm_min=26; int alarm_sec=0; Time tim = rtc.time(); snprintf(buf2, sizeof(buf2), "%02d:%02d:%02d", tim.hr, tim.min, tim.sec); if((tim.hr==alarm_hr)&&(tim.min==alarm_min)&&(tim.sec==alarm_sec)) { for(int i=0;i<20;i++) { digitalWrite(beep,LOW); delay(100); digitalWrite(beep,HIGH); delay(100); } } } void SerialTem() { int tol = dht.read(dht11Pin); int temp = (float)dht.temperature; int humi = (float)dht.humidity; Serial.print("Temperature:"); Serial.print(temp); Serial.println(".C"); Serial.print("Humidity:"); Serial.print(humi); Serial.println("%"); }
|